package msc.ai.crypto.jade;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.security.KeyStore;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Set;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SealedObject;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.PBEKeySpec;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.plaf.metal.MetalIconFactory.FolderIcon16;

public class PasswordPanel extends JFrame {
	private JLabel label2;
	JPasswordField tf ;
	private String dataStorage="./DataStorage/";
	private PaymentGatewayAgent myPGAgent;
	 public PasswordPanel(PaymentGatewayAgent pgAgent) {
	        this.myPGAgent=pgAgent;
	        setTitle("Payment Gateway loging");
	        JPanel panel = new JPanel();
	        JLabel label = new JLabel("Passward :");
	        
	         label2 = new JLabel("");
	         tf = new JPasswordField();
	        
	        tf.setColumns(10);
	        panel.add(label);
	        panel.add(tf);
	        panel.add(label2);
	        JButton button = new JButton();
	        button.setLabel("LOGING");
	        panel.add(button);
	        
	        
	        button .addActionListener( new ActionListener() 
			{
				public void actionPerformed(ActionEvent ev) 
				{
					SecretKey key =null ;
					try 
					{
						
						
						String pw= new String(tf.getPassword());
					
						 KeyStore privateKeyStore;
						privateKeyStore = KeyStore.getInstance("JCEKS");
						KeyGenerator generator = KeyGenerator.getInstance("AES");
						
						
						//Check the availability of the physically stored Key Store for given payment gateway agent 
						File file=new File(dataStorage+myPGAgent.getLocalName()+".auth");
						  boolean exists = file.exists();
						  if (!exists) {
							  File fld=new File(dataStorage);
							  
							  //if(!fld.exists()){
								  fld.mkdir();
								  //createNewFile();
							 // }
							  
								//No key store available; create a new one with the given password 
							  	key = generator.generateKey();
								privateKeyStore.load(null);
								privateKeyStore.setKeyEntry(myPGAgent.getLocalName(), key, pw.toCharArray(), null);
				
								FileOutputStream k_out = new FileOutputStream(dataStorage+
										myPGAgent.getLocalName()+".auth");
				
								
								ObjectOutputStream kob_out = new ObjectOutputStream(k_out);
				
								privateKeyStore.store(kob_out, pw.toCharArray());
							 
												  
							  
						  }else{
							  //Load the available key Store
							  ObjectInputStream kobj_in = null;
								
							
								FileInputStream k_in = new FileInputStream(dataStorage+myPGAgent.getLocalName()+".auth");
				
								kobj_in = new ObjectInputStream(k_in);
						  
								
									//Password validation and retrieving the secret key
									try {
										privateKeyStore.load(kobj_in, pw.toCharArray());
										key=(SecretKey) privateKeyStore.getKey(myPGAgent.getLocalName(), pw.toCharArray());
									} catch (Exception e) {
										
										label2.setText("Invalid Password");
										label2.setForeground(Color.RED);
										
										showGui();
										
									}
							  
						  }
			

						
						
						
						
						if(key!=null){
						PaymentGatewayGui pay = new PaymentGatewayGui(myPGAgent);
						 pay.showGui();
						 myPGAgent.setPgGui(pay);
						  myPGAgent.setKey(key);
						  dispose();
								  
						}
											
					}
					catch (Exception e) 
					{
						e.printStackTrace();
					}
				}
			} );
	        
	        getContentPane().add(panel, BorderLayout.SOUTH);
	        pack();
	        
	    }
	 
	 
	 
		public void showGui() {
			pack();
			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
			int centerX = (int)screenSize.getWidth() / 2;
			int centerY = (int)screenSize.getHeight() / 2;
			setLocation(centerX - getWidth() / 2, centerY - getHeight() / 2);
			super.setVisible(true);
			
			
		}
		
}
